<?php
extract(extractGet(), EXTR_PREFIX_ALL, 'c');
if ( isset($c_project_id) )
{

	$project = R::load('project', $c_project_id);
	if ( $project->board_id == null )
	{
		jsonErrorDie("Project does not belong to board.");
	}
	// Determining left...
	if ( ! isset($c_leftColumnId) )
	{
		// Finding leftmost and rightmost column of table
		$leftCol = R::findOne('column', 'board_id = ? AND boundary = -1', array($project->board_id));
	} else
	{
		$leftCol = R::load('column', $c_leftColumnId);
		if ( $leftCol->id == 0 )
		{
			jsonErrorDie("Cannot find left column with provided id.");
		}
	}
	//  ... and right column
	if ( ! isset($c_rightColumnId) )
	{
		$rightCol = R::findOne('column', 'board_id = ? AND boundary = 1', array($project->board_id));
	} else
	{
		$rightCol = R::load('column', $c_rightColumnId);
		if ( $rightCol->id == 0 )
		{
			jsonErrorDie("Cannot find right column with provided id.");
		}
	}

	$cards = R::findAll('card', 'project_id = ?', array($c_project_id));

	// ****************** FILTERS ********************
	// Filtering cards according to parameters

	// FIltering according to creation time
	if ( isset($c_dateCreatedFrom) && !empty($c_dateCreatedFrom))
	{
		if ( ! isset($c_dateCreatedTo) )
		{
			$c_dateCreatedTo = getDateWithDelay(0);
		}
		$cards = array_filter($cards, function ($bean) use ($c_dateCreatedFrom, $c_dateCreatedTo)
		{
			if ( (strtotime($bean->date_created) < strtotime($c_dateCreatedTo)) && strtotime($bean->date_created) > strtotime($c_dateCreatedFrom) )
			{

				return true;
			} else
			{
				return false;
			}
		});
	}

	// Filtering according to finished time
	if ( isset($c_dateFinishedFrom)  && !empty($c_dateFinishedFrom))
	{
            echo "ff: $c_dateFinishedFrom";
		if ( ! isset($c_dateFinishedTo) )
		{
			$c_dateFinishedTo = getDateWithDelay(0);
		}
		$cards = array_filter($cards, function ($bean) use ($c_dateFinishedFrom, $c_dateFinishedTo)
		{
			if ( (strtotime($bean->date_finished) < strtotime($c_dateFinishedTo)) && strtotime($bean->date_finished) > strtotime($c_dateFinishedFrom) )
			{

				return true;
			} else
			{
				return false;
			}
		});
	}

	// Filtering according to card size
	if ( isset ($c_sizeFrom)  && !empty($c_sizeFrom))
	{

		if ( !isset($c_sizeTo) || empty($c_sizeTo) || $c_sizeTo == 0)
		{
			$c_sizeTo = 100000000;
		}
		$cards = array_filter($cards, function ($bean) use ($c_sizeFrom, $c_sizeTo)
		{
			if ( ($bean->size < $c_sizeTo) && ($bean->size > $c_sizeFrom) )
			{
				return true;
			} else
			{
				return false;
			}
		});

	}

	// Filtering according to card type
	if ( isset($c_cardType) && !empty($c_cardType))
	{
            
                print_r($c_cardType);
		$cards = array_filter($cards, function ($bean) use ($c_cardType)
		{
                    
			return in_array($bean->type, $c_cardType);
		});
	}
	//else if(isset($c_project_id)){
	//    if(R::count('project', 'id = ? AND board_id = ?', array($c_project_id, $c_board_id)) == 0){
	//        jsonErrorDie("Project with provided id does not belong to provided table.");
	//    }
        if(count($cards) == 0){
            jsonErrorDie("No cards meet the conditions given.");
        }
	//print_r($cards);

//    foreach ($cards as $card) {
//        echo $card->title . "\n";
//    }    
} else
{
	jsonErrorDie("Check your parameters. All parameters are mandatory.");
}

include "graph/comulative.php";


// Average lead time
echo "<h2>Average lead time</h2>";
$avgTime = calculateAverageTime($cards, $leftCol, $rightCol);

if ( $avgTime == 0 )
{
	echo "<h4> Please check provided parameters, cannot calculate average lead time! Lead time equals 0. </h4>";
} else
{
	echo "<h4>Average lead time: <b>" . secondsToStringTime($avgTime) . "</b></h4><br/>";
}

//WIP volation
echo "<h2>Wip violation report</h2>";
$wipViolations = getCardWIPViolationLogs($cards);
if(empty($wipViolations)){
    echo "<h4>No wip violations on selected cards</h4>";
}
else{
    echo $wipViolations;
}


echo "Results are calculated on " . count($cards) ." cards.";




